<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/12/5
 * Time: 22:57
 */

namespace app\storage\service;

use app\storage\model\StorageModel;


class CommonService
{


    /**页面选择仓库下拉框
     * @param string $selectStorage
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_assign_storage($selectStorage = '')
    {
        $userId = session('ADMIN_ID');

        if ($userId == 1) {
            $storageModel = new StorageModel();
            $storage = $storageModel->field('uuid,name')->select();
        } else {
            $storageModel = new StorageModel();
            $storage = $storageModel->hasWhere('StaffStorage',['user_id'=>$userId])->select();
        }
        $count = count($storage);
        if (empty($count)) {
            $storage = [];
        } else if($count == 1){
            $storage = $storage->toArray();
        }else{
            $storage = $storage->toArray();
            array_unshift($storage, ['uuid' => '', 'name' => '所有']);
        }

        if (!empty($selectStorage)) {
            $index = array_search($selectStorage, array_column($storage, 'uuid'));
            array_unshift($storage, $storage[$index]);
            unset($storage[$index + 1]);
        }

        return $storage;
    }
}